package streaming.test.file;

import java.io.IOException;
import java.util.Scanner;

import javax.media.CannotRealizeException;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.DataSink;
import javax.media.EndOfMediaEvent;
import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoDataSinkException;
import javax.media.NoProcessorException;
import javax.media.Processor;
import javax.media.ProcessorModel;
import javax.media.datasink.DataSinkEvent;
import javax.media.datasink.DataSinkListener;
import javax.media.datasink.EndOfStreamEvent;
import javax.media.format.AudioFormat;
import javax.media.format.VideoFormat;
import javax.media.protocol.DataSource;
import javax.media.protocol.FileTypeDescriptor;

import sun.misc.CEStreamExhausted;

/**
 * works 
 * 
 * @author rashomon
 *
 */

public class MOVRecorder implements ControllerListener, DataSinkListener{

	Processor processor;
	DataSink dataSink;


	public MOVRecorder() {
		// TODO Auto-generated constructor stub
	}

	public void startRecording(){
		Format[] formats = new Format[]{
				new AudioFormat(AudioFormat.ULAW),
				new VideoFormat(VideoFormat.H263)
		};

		FileTypeDescriptor descriptor = new FileTypeDescriptor(FileTypeDescriptor.QUICKTIME);

		try {
			processor = Manager.createRealizedProcessor(new ProcessorModel(formats, descriptor));

			DataSource processorOutput = processor.getDataOutput();

			MediaLocator locator = new MediaLocator("file://mlif.mov");

			dataSink = Manager.createDataSink(processorOutput, locator);

			dataSink.addDataSinkListener(this);
			dataSink.open();
			dataSink.start();

			processor.start();


		} catch (NoProcessorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CannotRealizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoDataSinkException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

	private void finishRecording() {
		// TODO Auto-generated method stub
		processor.stop();
		processor.close();
	}

	public static void main(String[] args) {
//		Scanner sc = new Scanner(System.in);
//		String line = sc.nextLine();
		MOVRecorder recorder = new MOVRecorder();
//		while(!line.startsWith("quit")){
//			if(line.startsWith("record")){
//				recorder.startRecording();
//			}
//			line = sc.nextLine();
//		}
		recorder.startRecording();
		System.out.println("started");
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		recorder.finishRecording();
		System.out.println("Stoped");
	}

	@Override
	public void controllerUpdate(ControllerEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dataSinkUpdate(DataSinkEvent e) {
		// TODO Auto-generated method stub
		if(e instanceof EndOfStreamEvent){
			try {
				dataSink.stop();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			dataSink.close();
		}
	}



}
